
/*
* @Author: asus
* @Date:   2017-05-16 21:23:04
* @Last Modified by:   asus
* @Last Modified time: 2017-05-31 11:45:54
*/

'use strict';
require(["jquery-1.11.3","cookie"],function(){
	$(function(){
		//导航条下拉菜单
		$(".header_r>li").each(function(index, el) {
			$(this).hover(function() {
				$(this).find('.header_rs').css('display', 'block');
			}, function() {
				$(this).find('.header_rs').css('display', 'none');
			});
		});
		//表单框的验证	
		var oInput = document.getElementsByTagName("input");
		var oH6 = document.getElementsByTagName("h6");
		//表单验证
		//手机邮箱验证
		var re0 = /^.{5,20}$/;
		oInput[0].onblur = function(){
			if(oInput[0].value == ""){
				oH6[0].innerHTML = "不能输入为空，请重新输入";
			}
			else if(re0.test(oInput[0].value ) == false){
				oH6[0].innerHTML = "<i class = 'iconfont' >&#xe653;</i>请输入正确的邮箱/手机号";
				oH6[0].style.color = "red";
		 	}else{
		 		oH6[0].innerHTML = "<i class = 'iconfont' >&#xe654;</i>";
		 		oH6[0].style.color = "green";
		 	}
		};
		//密码验证
		var re1 = /^[A-Za-z0-9]{6,16}$/;
		//密码强度验证
		var re11 = /^[0-9]*$/;//密码强度为弱
		var re12 =  /^(?=.\d)(?=.*[a-z])|(?=.*[A-Z]).{6,16}$/;//密码强度为中
		var re13 = /^(?=.\d)(?=.*[a-z])(?=.*[A-Z]).{6,16}$/;//密码强度为强
		oInput[1].onblur = function(){
			if(oInput[1].value == ""){
				oH6[1].innerHTML = "不能输入为空，请重新输入";
			}
			else if(re1.test(oInput[1].value ) == false){
		 		oH6[1].innerHTML = "<i class = 'iconfont' >&#xe653;</i>请输入正确的密码";
		 		oH6[1].style.color = "red";
		 	}else{
		 		if(re11.test(oInput[1].value) == true){
		 			$(".strong>span").css({
		 				background: 'red'
		 			});
		 			$(".strong>span").html("弱");
		 		}
		 		if(re12.test(oInput[1].value) == true){
		 			$(".strong>span").css({
		 				background: 'yellow'
		 			});
		 			$(".strong>span").html("中");
		 		}
		 		if(re13.test(oInput[1].value) == true){
		 			$(".strong>span").css({
		 				background: 'green'
		 			});
		 			$(".strong>span").html("强");
		 		}
		 		oH6[1].innerHTML = "<i class = 'iconfont' >&#xe654;</i>";
		 		oH6[1].style.color = "green";
		 	}
		};
		
		//密码再次确认验证
		oInput[2].onblur = function(){
			if(oInput[2].value == ""){
				oH6[2].innerHTML = "不能输入为空，请重新输入";
			}
			else if(oInput[2].value !== oInput[1].value){
		 		oH6[2].innerHTML = "两次密码不一致，请重新输入";
		 	}else{
		 		oH6[2].innerHTML = "<i class ='iconfont'>&#xe654;</i>";
		 		oH6[2].style.color = "green";
		 	}
		};
		//随机验证码
		function fn(){
			var arr=["0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z","a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"];
			var str="";
			for(var j =1;j <= 4;j ++){
				var a=Math.floor(Math.random()*62);
				 str+=arr[a];
				$(".code>span").html(str);
			}
		}
		fn();
		//验证验证码的输入是否正确
		oInput[3].onblur = function(){
			if(oInput[3].value == ""){
				oH6[3].innerHTML = "验证码不能为空";
			}
			else if(oInput[3].value !== $(".code>span").html()){
		 		oH6[3].innerHTML = "验证码输入有误，请重新输入";
		 	}else{
		 		oH6[3].innerHTML = "<i class ='iconfont'>&#xe654;</i>";
		 		oH6[3].style.color = "green";
		 	}
		};
		//ajax局部更新验证码
		//点击看不清，换一张进行局部刷新
		$("#refreshB").click(function(event) {
			$.ajax({
				url: "sasaRegisterRefresh.html",
				cache: false,
				success: function(refresh){
				 	$("#refreshSpan").html(refresh);
				}
			});
		});
		//点击验证码进行刷新
		$("#refreshSpan").click(function(event) {
			$.ajax({
				url: "sasaRegisterRefresh.html",
				cache: false,
				success: function(refresh){
				 	$("#refreshSpan").html(refresh);
				}
			});
		});
		//获取页面元素
		var oRegister = document.getElementById("register");
		var oToLogin = document.getElementById("toLogin");
		//给注册按钮加事件
		oRegister.onclick = function(){
			//获取用户名和密码
			var username = oInput[0].value;
			var password = oInput[1].value;
			var passwordAgain = oInput[2].value; //确认密码
			//用户不能为空
			if(!username){
				alert("用户名不能为空！");
				return;
			}
			//检测密码是否相同
			//密码不能为空，密码规则
			if(password !== passwordAgain){
				alert("两次输入的密码不相同，请重试!");
				return;
			}
			//获取cookie中的用户信息
			var users = getCookie("registerUsers") ? getCookie("registerUsers") : "";
			//将字符串转为对象
			users = convertStrToObj(users);
			if(username in users){ //判断usn属性是否在users对象中。
				alert("用户名已经被注册");
				return;
			}else{
				//注册成功，设置用户信息的cookie
				//将用户添加到已注册用户列表对象中
				users[username] = password;
				//将用户信息对象转化回字符串，以便于设置cookie
				var userStr = convertObjToStr(users);
				//设置用户信息cookie
				setCookie("registerUsers",userStr,7);
				console.log(decodeURIComponent(document.cookie))
				alert("注册成功！");
				location.href = "../sasaIndex.html";
			}
		};
	});
	//将字符串转为对象
	function convertStrToObj(str){
		if(!str){
			return {};
		}
		var users = str.split(":"); //将字符串转为数组 
		var obj = {};
		//遍历数组
		for(var i = 0; i < users.length; i ++){
			//将字符串转为数组
			var userData = users[i].split(",");
			obj[userData[0]] = userData[1];
		}
		return obj;
	}
	//将对象转为字符串
	function convertObjToStr(obj){
		var str = "";
		for(var username in obj){
			var password = obj[username];
			if(str){
				//看是否是第一组用户名和密码，如果不是，先在前面添加一个：
				str += ":";
			}
			str += username + ',' + password;
		}
		return str;
	}


})
